#!/usr/bin/env python3
"""
YAML语法高亮器
支持YAML文件的高亮显示
"""

from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QColor

class YAMLHighlighter(QSyntaxHighlighter):
    """YAML语法高亮器"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.highlighting_rules = []
        
        # 关键字格式
        keyword_format = QTextCharFormat()
        keyword_format.setForeground(QColor(0, 0, 255))  # 蓝色
        keyword_format.setFontWeight(75)
        
        # 字符串格式
        string_format = QTextCharFormat()
        string_format.setForeground(QColor(163, 21, 21))  # 深红色
        
        # 数字格式
        number_format = QTextCharFormat()
        number_format.setForeground(QColor(9, 134, 88))  # 绿色
        
        # 注释格式
        comment_format = QTextCharFormat()
        comment_format.setForeground(QColor(0, 128, 0))  # 绿色
        comment_format.setFontItalic(True)
        
        # 键格式
        key_format = QTextCharFormat()
        key_format.setForeground(QColor(128, 0, 128))  # 紫色
        key_format.setFontWeight(75)
        
        # 特殊值格式
        special_format = QTextCharFormat()
        special_format.setForeground(QColor(128, 128, 0))  # 橄榄色
        
        # 添加规则
        
        # YAML关键字
        keywords = [
            r'true', r'false', r'null', r'True', r'False', r'Null',
            r'YES', r'NO', r'ON', r'OFF', r'y', r'n', r'yes', r'no',
            r'YAML', r'%YAML', r'%TAG'
        ]
        
        for keyword in keywords:
            pattern = QRegExp(r'\b' + keyword + r'\b')
            rule = (pattern, keyword_format)
            self.highlighting_rules.append(rule)
        
        # 数字
        rule = (QRegExp(r'\b\d+\.?\d*\b'), number_format)
        self.highlighting_rules.append(rule)
        
        # 注释
        rule = (QRegExp(r'#.*'), comment_format)
        self.highlighting_rules.append(rule)
        
        # 键（冒号前的内容）
        rule = (QRegExp(r'^\s*[^:#\n\r]+?(?=\s*:)'), key_format)
        self.highlighting_rules.append(rule)
        
        # 内联键
        rule = (QRegExp(r'\{[^}]*?([^:]+?):'), key_format)
        self.highlighting_rules.append(rule)
        
        # 双引号字符串
        rule = (QRegExp(r'".*?"'), string_format)
        self.highlighting_rules.append(rule)
        
        # 单引号字符串
        rule = (QRegExp(r"'.*?'"), string_format)
        self.highlighting_rules.append(rule)
        
        # 特殊字符
        special_chars = [r'---', r'\.\.\.', r'&', r'\*', r'<<', r'>>']
        for char in special_chars:
            pattern = QRegExp(char)
            rule = (pattern, special_format)
            self.highlighting_rules.append(rule)
        
        # 锚点和别名
        rule = (QRegExp(r'&\w+'), special_format)
        self.highlighting_rules.append(rule)
        rule = (QRegExp(r'\*\w+'), special_format)
        self.highlighting_rules.append(rule)
        
        # 数据类型标记
        rule = (QRegExp(r'!![^\s]+'), special_format)
        self.highlighting_rules.append(rule)
        
        # 多行字符串开始标记
        rule = (QRegExp(r'[|>]'), special_format)
        self.highlighting_rules.append(rule)
    
    def highlightBlock(self, text):
        """高亮文本块"""
        # 应用单行规则
        for pattern, format in self.highlighting_rules:
            expression = QRegExp(pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)